package com.hwx.controller;

import com.hwx.pojo.Member;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class MemberController {

    @RequestMapping("/getMember")
    public String getMember(){
        return "祥哥万岁";
    }

    @RequestMapping("/getMemberList")
    public List<Member> getMemberList(){
        List<Member> list = new ArrayList<Member>();

        list.add(new Member(1,"胡哥"));
        list.add(new Member(2,"文哥"));
        list.add(new Member(3,"祥哥"));

        return list;

    }

    @RequestMapping("/addMember")
    public void addMember(@RequestBody Member member){
       System.out.println("-***************************************************");
       System.out.println(member);
       System.out.println("-***************************************************");
    }

    @RequestMapping("/getMemberById")
    public String getMemberById(Integer id){
        return "尊敬的玛莎拉蒂车主:"+id;
    }

    /**
     * 根据ID查询会员信息，服务熔断的演示方法
     * @param id
     * @return
     * processHystrix  熔断的返回方法
     */
    @RequestMapping("/getMemberById2")
    @HystrixCommand(fallbackMethod = "processHystrix")
    public Member getMemberById2(Integer id){
        if(id == 0){
            throw new RuntimeException("该"+id+"存在异常！");
        }
        return new Member(id,"祥哥万岁");
    }

    public Member processHystrix(Integer id){
        return new Member(-1,"当前系统出现问题，请联系管理员！");
    }
}
